remotemanager.storage.sendablemixin module

This module handles the base functionality for serialising and unserialising objects.

The SendableMixin class provides the necessary methods to convert objects to YAML format and vice versa.

class remotemanager.storage.sendablemixin.SendableMixin[source]

Mixin class to create “sendable” object. Provides methods for conversion to yaml format

Create a sendable object by subclassing SendableMixin at class creation

>>> class MyObject(SendableMixin):
>>>     ...

Instances of this object will now have the required methods to be converted to and from dict format

>>> new = MyObject()
>>> payload = new.pack()  # store the object in a dict object
>>> recreated = MyObject.unpack(payload)  # create an instance from dict
pack(uuid: str = None, file: str = None) dict | None[source]

“packs” the object into a dict-format, ready for yaml-dump

Parameters:
  • uuid (str) –

    Optional uuid string to package this data inside. Adds a toplevel uuid to the payload dict: >>> p = {…}

    Becomes: >>> p = {uuid: {…}}

  • file (str) – Directly package to file with yaml.dump

Returns:

object payload

Return type:

(dict)

classmethod unpack(data: dict = None, file: str = None, **kwargs)[source]

Re-create an object from a packaged payload coming from obj.pack

Note

use this function to unpack from a payload _outside_ an object

newobj = MyObject.unpack(payload)

Where MyObject is a subclass of SendableMixin, and payload is a dict-type coming from MyObject.pack()

Parameters:
  • data (dict) – __dict__ payload from the object that was packaged

  • file (str) – filepath to unpack from, if data is not given

  • limit (bool) – set False to allow outside classes to be unserialised

Returns:

re-created object

inject_payload(payload: dict)[source]

inject payload into the __dict__, effectively re-creating the object

Note

use this function to unpack _within_ an object

>>> class MyObject(SendableMixin):
>>>     def __init__(self, ...):
>>>         ...
>>>         self.inject_payload(payload)
Parameters:

payload (dict) – __dict__ payload from the object that was packaged

serialise(obj)[source]

Recurse over any iterable objects, or call the pack() method of any SendableMixin objects, for serialisation

Parameters:

obj – object to be packaged

Returns (yaml-serialisable object):

yaml-friendly object

unserialise(obj)[source]

Undo a serialised packaging, by importing the called object and calling its unpack() method

Parameters:

obj – payload to be unpacked

Returns:

object before packaging

is_missing(objname: str) bool[source]

Determine if object with name objname is missing or uninitialised

Parameters:

objname (str) – name of the object to look for

Returns:

object presence

Return type:

(bool)

remotemanager.storage.sendablemixin.get_class_storage(obj) Dict[str, str][source]

Breaks down object into its module and classname.

Parameters:

obj – Python object to be broken down

Returns (dict):

module and classname dict

remotemanager.storage.sendablemixin.get_mro_classnames(obj) List[str][source]

Retrieves a list of class names from the Method Resolution Order (MRO) of an object.

Parameters:

obj – Python object whose MRO is to be retrieved.

Returns:

A list containing the class names in the order they appear in the MRO.

Return type:

List[str]

remotemanager.storage.sendablemixin.basic_available(obj) bool[source]

attempt a basic JSON serialisation, which will fail fast if we can’t dump